﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.InteropServices;

    public class CategoryController
    {
        public void ClearCategory(int CategoryID)
        {
            DataProvider.Instance().ClearCategory(CategoryID);
        }

        public void CopyToLanguages(NB_Store_CategoriesInfo objInfo, bool ForceOverwrite = true)
        {
            IEnumerator enumerator=null;
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            try
            {
                enumerator = validLocales.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string lang = Conversions.ToString(enumerator.Current);
                    this.CopyToLanguages(objInfo, lang, ForceOverwrite);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void CopyToLanguages(NB_Store_CategoriesInfo objInfo, string Lang, bool ForceOverwrite = true)
        {
            bool flag = true;
            if (!ForceOverwrite)
            {
                NB_Store_CategoriesInfo category = this.GetCategory(objInfo.CategoryID, Lang);
                if (category == null)
                {
                    flag = true;
                }
                else if (category.CategoryName == "")
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            if (flag)
            {
                objInfo.Lang = Lang;
                this.UpdateObjCategories(objInfo);
            }
        }

        public void DeleteCategories(int CategoryID)
        {
            DataProvider.Instance().DeleteNB_Store_Categories(CategoryID);
        }

        public bool DependantExists(int PortalID, string Lang, int CategoryID)
        {
            IEnumerator enumerator=null;
            bool flag2 = false;
            ArrayList categories = this.GetCategories(PortalID, Lang);
            try
            {
                enumerator = categories.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator.Current;
                    if (current.ParentCategoryID == CategoryID)
                    {
                        flag2 = true;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            ProductController controller = new ProductController();
            if (controller.GetProductList(PortalID, CategoryID, Lang, 1, 1, true).Count > 0)
            {
                flag2 = true;
            }
            return flag2;
        }

        public ArrayList GetCategories(int PortalID, string Lang)
        {
            return this.GetCategories(PortalID, Lang, -1, false, true);
        }

        public ArrayList GetCategories(int PortalID, string Lang, int ParentID)
        {
            return this.GetCategories(PortalID, Lang, ParentID, false, true);
        }

        public ArrayList GetCategories(int PortalID, string Lang, int ParentID, bool Archived, bool IncludeArchived)
        {
            return CBO.FillCollection(DataProvider.Instance().GetNB_Store_CategoriesList(PortalID, Lang, ParentID, Archived, IncludeArchived), typeof(NB_Store_CategoriesInfo));
        }

        public Hashtable GetCategoriesTable(ArrayList aryList)
        {
            IEnumerator enumerator=null;
            Hashtable hashtable2 = new Hashtable();
            try
            {
                enumerator = aryList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator.Current;
                    hashtable2.Add(current.CategoryID, current.CategoryName);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return hashtable2;
        }

        public Hashtable GetCategoriesTable(int PortalID, string Lang)
        {
            ArrayList categories = this.GetCategories(PortalID, Lang);
            return this.GetCategoriesTable(categories);
        }

        public Hashtable GetCategoriesTable(int PortalID, string Lang, int ParentID)
        {
            ArrayList aryList = this.GetCategories(PortalID, Lang, ParentID);
            return this.GetCategoriesTable(aryList);
        }

        public NB_Store_CategoriesInfo GetCategory(int CategoryID, string Lang)
        {
            return (NB_Store_CategoriesInfo) CBO.FillObject(DataProvider.Instance().GetNB_Store_Categories(CategoryID, Lang), typeof(NB_Store_CategoriesInfo));
        }

        public Collection GetSubCategoryCollection(int PortalID, int ParentID)
        {
            Collection subCatCollection = new Collection();
            ArrayList categories = this.GetCategories(PortalID, SharedFunctions.GetCurrentCulture());
            return this.GetSubCategoryCollection(categories, ParentID, subCatCollection);
        }

        public Collection GetSubCategoryCollection(ArrayList aryList, int ParentID, Collection SubCatCollection)
        {
            IEnumerator enumerator=null;
            try
            {
                enumerator = aryList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CategoriesInfo current = (NB_Store_CategoriesInfo) enumerator.Current;
                    if ((current.ParentCategoryID == ParentID) & !current.Archived)
                    {
                        if (!SubCatCollection.Contains(Conversions.ToString(current.CategoryID)))
                        {
                            SubCatCollection.Add(current.CategoryID, Conversions.ToString(current.CategoryID), null, null);
                            SubCatCollection = this.GetSubCategoryCollection(aryList, current.CategoryID, SubCatCollection);
                        }
                        else
                        {
                            NB_Store_CategoriesInfo category = this.GetCategory(ParentID, SharedFunctions.GetCurrentCulture());
                            if (category != null)
                            {
                                SharedFunctions.UpdateLog("FIXED RECURSIVE CATEGORY: " + category.CategoryName + " CatID: " + Conversions.ToString(category.CategoryID) + "ParentID: " + Conversions.ToString(category.ParentCategoryID));
                                category.ParentCategoryID = 0;
                                this.UpdateObjCategories(category);
                            }
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return SubCatCollection;
        }

        public string GetSubCategoryList(ArrayList aryList, int ParentID)
        {
            Collection subCatCollection = new Collection();
            string left = "";
            subCatCollection = this.GetSubCategoryCollection(aryList, ParentID, subCatCollection);
            int count = subCatCollection.Count;
            for (int i = 1; i <= count; i++)
            {
                left = Conversions.ToString(Operators.ConcatenateObject(left, Operators.ConcatenateObject(subCatCollection[i], ",")));
            }
            return left;
        }

        public void MoveCategoryProducts(int FromCatID, int ToCatID, bool RemoveFromCat)
        {
            ProductController controller2 = new ProductController();
            CategoryController controller = new CategoryController();
            if (controller.GetCategory(FromCatID, SharedFunctions.GetCurrentCulture()) != null)
            {
                NB_Store_CategoriesInfo category = controller.GetCategory(ToCatID, SharedFunctions.GetCurrentCulture());
                if (category != null)
                {
                    IEnumerator enumerator=null;
                    ArrayList list = controller2.GetProductList(category.PortalID, FromCatID, SharedFunctions.GetCurrentCulture(), "", true, false, true);
                    try
                    {
                        enumerator = list.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            ProductListInfo current = (ProductListInfo) enumerator.Current;
                            if (RemoveFromCat)
                            {
                                controller2.DeleteProductCategory(current.ProductID, FromCatID);
                            }
                            controller2.UpdateProductCategory(current.ProductID, ToCatID);
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }
                }
            }
        }

        public NB_Store_CategoriesInfo UpdateObjCategories(NB_Store_CategoriesInfo objInfo)
        {
            return (NB_Store_CategoriesInfo) CBO.FillObject(DataProvider.Instance().UpdateNB_Store_Categories(objInfo.CategoryID, objInfo.Lang, objInfo.CategoryName, objInfo.CategoryDesc, objInfo.Message, objInfo.PortalID, objInfo.Archived, objInfo.CreatedByUser, objInfo.CreatedDate, objInfo.ParentCategoryID, objInfo.ListOrder, objInfo.ProductTemplate, objInfo.ListItemTemplate, objInfo.ListAltItemTemplate, objInfo.Hide, objInfo.ImageURL, objInfo.SEOPageTitle, objInfo.SEOName, objInfo.MetaDescription, objInfo.MetaKeywords), typeof(NB_Store_CategoriesInfo));
        }
    }
}

